<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
        <title>
            
    
                Declarative
             &mdash; 
    SQLAlchemy 0.8 Documentation

        </title>
        
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/docs.css" type="text/css" />

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
          URL_ROOT:    '../../',
          VERSION:     '0.8.2',
          COLLAPSE_MODINDEX: false,
          FILE_SUFFIX: '.html'
      };
    </script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/init.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
        <link rel="copyright" title="Copyright" href="../../copyright.html" />
    <link rel="top" title="SQLAlchemy 0.8 Documentation" href="../../index.html" />
        <link rel="up" title="ORM Extensions" href="index.html" />
        <link rel="next" title="Mutation Tracking" href="mutable.html" />
        <link rel="prev" title="Association Proxy" href="associationproxy.html" />

    </head>
    <body>
        










<div id="docs-container">



<div id="docs-header">
    <h1>SQLAlchemy 0.8 Documentation</h1>

    <div id="docs-search">
    Search:
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>

    <div id="docs-version-header">
        Release: <span class="version-num">0.8.2</span> | Release Date: July 3, 2013


    </div>

</div>

<div id="docs-top-navigation">
    <div id="docs-top-page-control" class="docs-navigation-links">
        <ul>
            <li>Prev:
            <a href="associationproxy.html" title="previous chapter">Association Proxy</a>
            </li>
            <li>Next:
            <a href="mutable.html" title="next chapter">Mutation Tracking</a>
            </li>

        <li>
            <a href="../../contents.html">Table of Contents</a> |
            <a href="../../genindex.html">Index</a>
            | <a href="../../_sources/orm/extensions/declarative.txt">view source
        </li>
        </ul>
    </div>

    <div id="docs-navigation-banner">
        <a href="../../index.html">SQLAlchemy 0.8 Documentation</a>
                » <a href="../index.html" title="SQLAlchemy ORM">SQLAlchemy ORM</a>
                » <a href="index.html" title="ORM Extensions">ORM Extensions</a>
        » 
                Declarative
             

        <h2>
            
                Declarative
            
        </h2>
    </div>

</div>

<div id="docs-body-container">

    <div id="docs-sidebar">
    <h3><a href="../../index.html">Table of Contents</a></h3>
    <ul>
<li><a class="reference internal" href="#">Declarative</a><ul>
<li><a class="reference internal" href="#synopsis">Synopsis</a></li>
<li><a class="reference internal" href="#defining-attributes">Defining Attributes</a></li>
<li><a class="reference internal" href="#accessing-the-metadata">Accessing the MetaData</a></li>
<li><a class="reference internal" href="#configuring-relationships">Configuring Relationships</a></li>
<li><a class="reference internal" href="#configuring-many-to-many-relationships">Configuring Many-to-Many Relationships</a></li>
<li><a class="reference internal" href="#defining-sql-expressions">Defining SQL Expressions</a></li>
<li><a class="reference internal" href="#table-configuration">Table Configuration</a></li>
<li><a class="reference internal" href="#using-a-hybrid-approach-with-table">Using a Hybrid Approach with __table__</a></li>
<li><a class="reference internal" href="#using-reflection-with-declarative">Using Reflection with Declarative</a></li>
<li><a class="reference internal" href="#mapper-configuration">Mapper Configuration</a></li>
<li><a class="reference internal" href="#inheritance-configuration">Inheritance Configuration</a><ul>
<li><a class="reference internal" href="#joined-table-inheritance">Joined Table Inheritance</a></li>
<li><a class="reference internal" href="#single-table-inheritance">Single Table Inheritance</a><ul>
<li><a class="reference internal" href="#resolving-column-conflicts">Resolving Column Conflicts</a></li>
</ul>
</li>
<li><a class="reference internal" href="#concrete-table-inheritance">Concrete Table Inheritance</a><ul>
<li><a class="reference internal" href="#using-the-concrete-helpers">Using the Concrete Helpers</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#mixin-and-custom-base-classes">Mixin and Custom Base Classes</a><ul>
<li><a class="reference internal" href="#augmenting-the-base">Augmenting the Base</a></li>
<li><a class="reference internal" href="#mixing-in-columns">Mixing in Columns</a></li>
<li><a class="reference internal" href="#mixing-in-relationships">Mixing in Relationships</a></li>
<li><a class="reference internal" href="#mixing-in-deferred-column-property-and-other-mapperproperty-classes">Mixing in deferred(), column_property(), and other MapperProperty classes</a></li>
<li><a class="reference internal" href="#mixing-in-association-proxy-and-other-attributes">Mixing in Association Proxy and Other Attributes</a></li>
<li><a class="reference internal" href="#controlling-table-inheritance-with-mixins">Controlling table inheritance with mixins</a></li>
<li><a class="reference internal" href="#combining-table-mapper-arguments-from-multiple-mixins">Combining Table/Mapper Arguments from Multiple Mixins</a></li>
<li><a class="reference internal" href="#creating-indexes-with-mixins">Creating Indexes with Mixins</a></li>
</ul>
</li>
<li><a class="reference internal" href="#special-directives">Special Directives</a><ul>
<li><a class="reference internal" href="#declare-last"><tt class="docutils literal"><span class="pre">__declare_last__()</span></tt></a></li>
<li><a class="reference internal" href="#abstract"><tt class="docutils literal"><span class="pre">__abstract__</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#class-constructor">Class Constructor</a></li>
<li><a class="reference internal" href="#sessions">Sessions</a></li>
<li><a class="reference internal" href="#api-reference">API Reference</a></li>
</ul>
</li>
</ul>


    <h4>Previous Topic</h4>
    <p>
    <a href="associationproxy.html" title="previous chapter">Association Proxy</a>
    </p>
    <h4>Next Topic</h4>
    <p>
    <a href="mutable.html" title="next chapter">Mutation Tracking</a>
    </p>


    <h4>Quick Search</h4>
    <p>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </p>

    </div>

    <div id="docs-body" class="withsidebar" >
        
<div class="section" id="module-sqlalchemy.ext.declarative">
<span id="declarative"></span><span id="declarative-toplevel"></span><h1>Declarative<a class="headerlink" href="#module-sqlalchemy.ext.declarative" title="Permalink to this headline">¶</a></h1>
<div class="section" id="synopsis">
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Permalink to this headline">¶</a></h2>
<p>SQLAlchemy object-relational configuration involves the
combination of <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>, <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a>, and class
objects to define a mapped class.
<a class="reference internal" href="#module-sqlalchemy.ext.declarative" title="sqlalchemy.ext.declarative"><tt class="xref py py-mod docutils literal"><span class="pre">declarative</span></tt></a> allows all three to be
expressed at once within the class declaration. As much as
possible, regular SQLAlchemy schema and ORM constructs are
used directly, so that configuration between &#8220;classical&#8221; ORM
usage and declarative remain highly similar.</p>
<p>As a simple example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>

<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">SomeClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;some_table&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span></pre></div>
</div>
<p>Above, the <a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a> callable returns a new base class from
which all mapped classes should inherit. When the class definition is
completed, a new <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> and <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a> will have been generated.</p>
<p>The resulting table and mapper are accessible via
<tt class="docutils literal"><span class="pre">__table__</span></tt> and <tt class="docutils literal"><span class="pre">__mapper__</span></tt> attributes on the
<tt class="docutils literal"><span class="pre">SomeClass</span></tt> class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># access the mapped Table</span>
<span class="n">SomeClass</span><span class="o">.</span><span class="n">__table__</span>

<span class="c"># access the Mapper</span>
<span class="n">SomeClass</span><span class="o">.</span><span class="n">__mapper__</span></pre></div>
</div>
</div>
<div class="section" id="defining-attributes">
<h2>Defining Attributes<a class="headerlink" href="#defining-attributes" title="Permalink to this headline">¶</a></h2>
<p>In the previous example, the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects are
automatically named with the name of the attribute to which they are
assigned.</p>
<p>To name columns explicitly with a name distinct from their mapped attribute,
just give the column a name.  Below, column &#8220;some_table_id&#8221; is mapped to the
&#8220;id&#8221; attribute of <cite>SomeClass</cite>, but in SQL will be represented as
&#8220;some_table_id&#8221;:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">SomeClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;some_table&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&quot;some_table_id&quot;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
<p>Attributes may be added to the class after its construction, and they will be
added to the underlying <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> and
<a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a> definitions as appropriate:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">SomeClass</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;data&#39;</span><span class="p">,</span> <span class="n">Unicode</span><span class="p">)</span>
<span class="n">SomeClass</span><span class="o">.</span><span class="n">related</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="n">RelatedInfo</span><span class="p">)</span></pre></div>
</div>
<p>Classes which are constructed using declarative can interact freely
with classes that are mapped explicitly with <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a>.</p>
<p>It is recommended, though not required, that all tables
share the same underlying <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object,
so that string-configured <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>
references can be resolved without issue.</p>
</div>
<div class="section" id="accessing-the-metadata">
<h2>Accessing the MetaData<a class="headerlink" href="#accessing-the-metadata" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a> base class contains a
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object where newly defined
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects are collected. This object is
intended to be accessed directly for
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>-specific operations. Such as, to issue
CREATE statements for all tables:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;sqlite://&#39;</span><span class="p">)</span>
<span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span></pre></div>
</div>
<p><a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a> can also receive a pre-existing
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object, which allows a
declarative setup to be associated with an already
existing traditional collection of <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
objects:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">mymetadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">(</span><span class="n">metadata</span><span class="o">=</span><span class="n">mymetadata</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="configuring-relationships">
<h2>Configuring Relationships<a class="headerlink" href="#configuring-relationships" title="Permalink to this headline">¶</a></h2>
<p>Relationships to other classes are done in the usual way, with the added
feature that the class specified to <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>
may be a string name.  The &#8220;class registry&#8221; associated with <tt class="docutils literal"><span class="pre">Base</span></tt>
is used at mapper compilation time to resolve the name into the actual
class object, which is expected to have been defined once the mapper
configuration is used:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;users&#39;</span>

    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">addresses</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Address&quot;</span><span class="p">,</span> <span class="n">backref</span><span class="o">=</span><span class="s">&quot;user&quot;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Address</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;addresses&#39;</span>

    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">email</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">user_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;users.id&#39;</span><span class="p">))</span></pre></div>
</div>
<p>Column constructs, since they are just that, are immediately usable,
as below where we define a primary join condition on the <tt class="docutils literal"><span class="pre">Address</span></tt>
class using them:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Address</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;addresses&#39;</span>

    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">email</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">user_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;users.id&#39;</span><span class="p">))</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="n">User</span><span class="p">,</span> <span class="n">primaryjoin</span><span class="o">=</span><span class="n">user_id</span> <span class="o">==</span> <span class="n">User</span><span class="o">.</span><span class="n">id</span><span class="p">)</span></pre></div>
</div>
<p>In addition to the main argument for <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>,
other arguments which depend upon the columns present on an as-yet
undefined class may also be specified as strings.  These strings are
evaluated as Python expressions.  The full namespace available within
this evaluation includes all classes mapped for this declarative base,
as well as the contents of the <tt class="docutils literal"><span class="pre">sqlalchemy</span></tt> package, including
expression functions like <a class="reference internal" href="../../core/expression_api.html#sqlalchemy.sql.expression.desc" title="sqlalchemy.sql.expression.desc"><tt class="xref py py-func docutils literal"><span class="pre">desc()</span></tt></a> and
<a class="reference internal" href="../../core/expression_api.html#sqlalchemy.sql.expression.func" title="sqlalchemy.sql.expression.func"><tt class="xref py py-attr docutils literal"><span class="pre">func</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="c"># ....</span>
    <span class="n">addresses</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Address&quot;</span><span class="p">,</span>
                         <span class="n">order_by</span><span class="o">=</span><span class="s">&quot;desc(Address.email)&quot;</span><span class="p">,</span>
                         <span class="n">primaryjoin</span><span class="o">=</span><span class="s">&quot;Address.user_id==User.id&quot;</span><span class="p">)</span></pre></div>
</div>
<p>For the case where more than one module contains a class of the same name,
string class names can also be specified as module-qualified paths
within any of these string expressions:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="c"># ....</span>
    <span class="n">addresses</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;myapp.model.address.Address&quot;</span><span class="p">,</span>
                         <span class="n">order_by</span><span class="o">=</span><span class="s">&quot;desc(myapp.model.address.Address.email)&quot;</span><span class="p">,</span>
                         <span class="n">primaryjoin</span><span class="o">=</span><span class="s">&quot;myapp.model.address.Address.user_id==&quot;</span>
                                        <span class="s">&quot;myapp.model.user.User.id&quot;</span><span class="p">)</span></pre></div>
</div>
<p>The qualified path can be any partial path that removes ambiguity between
the names.  For example, to disambiguate between
<tt class="docutils literal"><span class="pre">myapp.model.address.Address</span></tt> and <tt class="docutils literal"><span class="pre">myapp.model.lookup.Address</span></tt>,
we can specify <tt class="docutils literal"><span class="pre">address.Address</span></tt> or <tt class="docutils literal"><span class="pre">lookup.Address</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="c"># ....</span>
    <span class="n">addresses</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;address.Address&quot;</span><span class="p">,</span>
                         <span class="n">order_by</span><span class="o">=</span><span class="s">&quot;desc(address.Address.email)&quot;</span><span class="p">,</span>
                         <span class="n">primaryjoin</span><span class="o">=</span><span class="s">&quot;address.Address.user_id==&quot;</span>
                                        <span class="s">&quot;User.id&quot;</span><span class="p">)</span></pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 0.8: </span>module-qualified paths can be used when specifying string arguments
with Declarative, in order to specify specific modules.</p>
<p>Two alternatives also exist to using string-based attributes.  A lambda
can also be used, which will be evaluated after all mappers have been
configured:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="c"># ...</span>
    <span class="n">addresses</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="n">Address</span><span class="p">,</span>
                         <span class="n">order_by</span><span class="o">=</span><span class="k">lambda</span><span class="p">:</span> <span class="n">desc</span><span class="p">(</span><span class="n">Address</span><span class="o">.</span><span class="n">email</span><span class="p">),</span>
                         <span class="n">primaryjoin</span><span class="o">=</span><span class="k">lambda</span><span class="p">:</span> <span class="n">Address</span><span class="o">.</span><span class="n">user_id</span><span class="o">==</span><span class="n">User</span><span class="o">.</span><span class="n">id</span><span class="p">)</span></pre></div>
</div>
<p>Or, the relationship can be added to the class explicitly after the classes
are available:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">User</span><span class="o">.</span><span class="n">addresses</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="n">Address</span><span class="p">,</span>
                          <span class="n">primaryjoin</span><span class="o">=</span><span class="n">Address</span><span class="o">.</span><span class="n">user_id</span><span class="o">==</span><span class="n">User</span><span class="o">.</span><span class="n">id</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="configuring-many-to-many-relationships">
<h2>Configuring Many-to-Many Relationships<a class="headerlink" href="#configuring-many-to-many-relationships" title="Permalink to this headline">¶</a></h2>
<p>Many-to-many relationships are also declared in the same way
with declarative as with traditional mappings. The
<tt class="docutils literal"><span class="pre">secondary</span></tt> argument to
<a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a> is as usual passed a
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object, which is typically declared in the
traditional way.  The <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> usually shares
the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object used by the declarative base:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">keywords</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span>
    <span class="s">&#39;keywords&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;author_id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;authors.id&#39;</span><span class="p">)),</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;keyword_id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;keywords.id&#39;</span><span class="p">))</span>
    <span class="p">)</span>

<span class="k">class</span> <span class="nc">Author</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;authors&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">keywords</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Keyword&quot;</span><span class="p">,</span> <span class="n">secondary</span><span class="o">=</span><span class="n">keywords</span><span class="p">)</span></pre></div>
</div>
<p>Like other <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a> arguments, a string is accepted
as well, passing the string name of the table as defined in the
<tt class="docutils literal"><span class="pre">Base.metadata.tables</span></tt> collection:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Author</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;authors&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">keywords</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Keyword&quot;</span><span class="p">,</span> <span class="n">secondary</span><span class="o">=</span><span class="s">&quot;keywords&quot;</span><span class="p">)</span></pre></div>
</div>
<p>As with traditional mapping, its generally not a good idea to use
a <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> as the &#8220;secondary&#8221; argument which is also mapped to
a class, unless the <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a> is declared with <tt class="docutils literal"><span class="pre">viewonly=True</span></tt>.
Otherwise, the unit-of-work system may attempt duplicate INSERT and
DELETE statements against the underlying table.</p>
</div>
<div class="section" id="defining-sql-expressions">
<span id="declarative-sql-expressions"></span><h2>Defining SQL Expressions<a class="headerlink" href="#defining-sql-expressions" title="Permalink to this headline">¶</a></h2>
<p>See <a class="reference internal" href="../mapper_config.html#mapper-sql-expressions"><em>SQL Expressions as Mapped Attributes</em></a> for examples on declaratively
mapping attributes to SQL expressions.</p>
</div>
<div class="section" id="table-configuration">
<span id="declarative-table-args"></span><h2>Table Configuration<a class="headerlink" href="#table-configuration" title="Permalink to this headline">¶</a></h2>
<p>Table arguments other than the name, metadata, and mapped Column
arguments are specified using the <tt class="docutils literal"><span class="pre">__table_args__</span></tt> class attribute.
This attribute accommodates both positional as well as keyword
arguments that are normally sent to the
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> constructor.
The attribute can be specified in one of two forms. One is as a
dictionary:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;sometable&#39;</span>
    <span class="n">__table_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;mysql_engine&#39;</span><span class="p">:</span><span class="s">&#39;InnoDB&#39;</span><span class="p">}</span></pre></div>
</div>
<p>The other, a tuple, where each argument is positional
(usually constraints):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;sometable&#39;</span>
    <span class="n">__table_args__</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">ForeignKeyConstraint</span><span class="p">([</span><span class="s">&#39;id&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s">&#39;remote_table.id&#39;</span><span class="p">]),</span>
            <span class="n">UniqueConstraint</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">),</span>
            <span class="p">)</span></pre></div>
</div>
<p>Keyword arguments can be specified with the above form by
specifying the last argument as a dictionary:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;sometable&#39;</span>
    <span class="n">__table_args__</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">ForeignKeyConstraint</span><span class="p">([</span><span class="s">&#39;id&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s">&#39;remote_table.id&#39;</span><span class="p">]),</span>
            <span class="n">UniqueConstraint</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">),</span>
            <span class="p">{</span><span class="s">&#39;autoload&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span>
            <span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="using-a-hybrid-approach-with-table">
<h2>Using a Hybrid Approach with __table__<a class="headerlink" href="#using-a-hybrid-approach-with-table" title="Permalink to this headline">¶</a></h2>
<p>As an alternative to <tt class="docutils literal"><span class="pre">__tablename__</span></tt>, a direct
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> construct may be used.  The
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects, which in this case require
their names, will be added to the mapping just like a regular mapping
to a table:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__table__</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;my_table&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="p">)</span></pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">__table__</span></tt> provides a more focused point of control for establishing
table metadata, while still getting most of the benefits of using declarative.
An application that uses reflection might want to load table metadata elsewhere
and pass it to declarative classes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>

<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>
<span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">reflect</span><span class="p">(</span><span class="n">some_engine</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__table__</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="n">tables</span><span class="p">[</span><span class="s">&#39;user&#39;</span><span class="p">]</span>

<span class="k">class</span> <span class="nc">Address</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__table__</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="n">tables</span><span class="p">[</span><span class="s">&#39;address&#39;</span><span class="p">]</span></pre></div>
</div>
<p>Some configuration schemes may find it more appropriate to use <tt class="docutils literal"><span class="pre">__table__</span></tt>,
such as those which already take advantage of the data-driven nature of
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> to customize and/or automate schema definition.</p>
<p>Note that when the <tt class="docutils literal"><span class="pre">__table__</span></tt> approach is used, the object is immediately
usable as a plain <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> within the class declaration body itself,
as a Python class is only another syntactical block.  Below this is illustrated
by using the <tt class="docutils literal"><span class="pre">id</span></tt> column in the <tt class="docutils literal"><span class="pre">primaryjoin</span></tt> condition of a
<a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__table__</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;my_table&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="p">)</span>

    <span class="n">widgets</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="n">Widget</span><span class="p">,</span>
                <span class="n">primaryjoin</span><span class="o">=</span><span class="n">Widget</span><span class="o">.</span><span class="n">myclass_id</span><span class="o">==</span><span class="n">__table__</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">)</span></pre></div>
</div>
<p>Similarly, mapped attributes which refer to <tt class="docutils literal"><span class="pre">__table__</span></tt> can be placed inline,
as below where we assign the <tt class="docutils literal"><span class="pre">name</span></tt> column to the attribute <tt class="docutils literal"><span class="pre">_name</span></tt>,
generating a synonym for <tt class="docutils literal"><span class="pre">name</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">synonym_for</span>

<span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__table__</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;my_table&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="p">)</span>

    <span class="n">_name</span> <span class="o">=</span> <span class="n">__table__</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">name</span>

    <span class="nd">@synonym_for</span><span class="p">(</span><span class="s">&quot;_name&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&quot;Name: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">_name</span></pre></div>
</div>
</div>
<div class="section" id="using-reflection-with-declarative">
<h2>Using Reflection with Declarative<a class="headerlink" href="#using-reflection-with-declarative" title="Permalink to this headline">¶</a></h2>
<p>It&#8217;s easy to set up a <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> that uses <tt class="docutils literal"><span class="pre">autoload=True</span></tt>
in conjunction with a mapped class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__table__</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;mytable&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
                    <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">autoload_with</span><span class="o">=</span><span class="n">some_engine</span><span class="p">)</span></pre></div>
</div>
<p>However, one improvement that can be made here is to not
require the <a class="reference internal" href="../../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> to be available when classes are
being first declared.   To achieve this, use the
<a class="reference internal" href="#sqlalchemy.ext.declarative.DeferredReflection" title="sqlalchemy.ext.declarative.DeferredReflection"><tt class="xref py py-class docutils literal"><span class="pre">DeferredReflection</span></tt></a> mixin, which sets up mappings
only after a special <tt class="docutils literal"><span class="pre">prepare(engine)</span></tt> step is called:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span><span class="p">,</span> <span class="n">DeferredReflection</span>

<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">(</span><span class="n">cls</span><span class="o">=</span><span class="n">DeferredReflection</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;foo&#39;</span>
    <span class="n">bars</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Bar&quot;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Bar</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;bar&#39;</span>

    <span class="c"># illustrate overriding of &quot;bar.foo_id&quot; to have</span>
    <span class="c"># a foreign key constraint otherwise not</span>
    <span class="c"># reflected, such as when using MySQL</span>
    <span class="n">foo_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;foo.id&#39;</span><span class="p">))</span>

<span class="n">Base</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span><span class="n">e</span><span class="p">)</span></pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 0.8: </span>Added <a class="reference internal" href="#sqlalchemy.ext.declarative.DeferredReflection" title="sqlalchemy.ext.declarative.DeferredReflection"><tt class="xref py py-class docutils literal"><span class="pre">DeferredReflection</span></tt></a>.</p>
</div>
<div class="section" id="mapper-configuration">
<h2>Mapper Configuration<a class="headerlink" href="#mapper-configuration" title="Permalink to this headline">¶</a></h2>
<p>Declarative makes use of the <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a> function internally
when it creates the mapping to the declared table.   The options
for <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a> are passed directly through via the
<tt class="docutils literal"><span class="pre">__mapper_args__</span></tt> class attribute.  As always, arguments which reference
locally mapped columns can reference them directly from within the
class declaration:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>

<span class="k">class</span> <span class="nc">Widget</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;widgets&#39;</span>

    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">timestamp</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">,</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s">&#39;version_id_col&#39;</span><span class="p">:</span> <span class="n">timestamp</span><span class="p">,</span>
                    <span class="s">&#39;version_id_generator&#39;</span><span class="p">:</span> <span class="k">lambda</span> <span class="n">v</span><span class="p">:</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
                <span class="p">}</span></pre></div>
</div>
</div>
<div class="section" id="inheritance-configuration">
<span id="declarative-inheritance"></span><h2>Inheritance Configuration<a class="headerlink" href="#inheritance-configuration" title="Permalink to this headline">¶</a></h2>
<p>Declarative supports all three forms of inheritance as intuitively
as possible.  The <tt class="docutils literal"><span class="pre">inherits</span></tt> mapper keyword argument is not needed
as declarative will determine this from the class itself.   The various
&#8220;polymorphic&#8221; keyword arguments are specified using <tt class="docutils literal"><span class="pre">__mapper_args__</span></tt>.</p>
<div class="section" id="joined-table-inheritance">
<h3>Joined Table Inheritance<a class="headerlink" href="#joined-table-inheritance" title="Permalink to this headline">¶</a></h3>
<p>Joined table inheritance is defined as a subclass that defines its own
table:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;people&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">discriminator</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span> <span class="n">discriminator</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;engineers&#39;</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;people.id&#39;</span><span class="p">),</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">primary_language</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span></pre></div>
</div>
<p>Note that above, the <tt class="docutils literal"><span class="pre">Engineer.id</span></tt> attribute, since it shares the
same attribute name as the <tt class="docutils literal"><span class="pre">Person.id</span></tt> attribute, will in fact
represent the <tt class="docutils literal"><span class="pre">people.id</span></tt> and <tt class="docutils literal"><span class="pre">engineers.id</span></tt> columns together,
with the &#8220;Engineer.id&#8221; column taking precedence if queried directly.
To provide the <tt class="docutils literal"><span class="pre">Engineer</span></tt> class with an attribute that represents
only the <tt class="docutils literal"><span class="pre">engineers.id</span></tt> column, give it a different attribute name:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;engineers&#39;</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span>
    <span class="n">engineer_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;people.id&#39;</span><span class="p">),</span>
                                                <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">primary_language</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span></pre></div>
</div>
<p class="versionchanged">
<span class="versionmodified">Changed in version 0.7: </span>joined table inheritance favors the subclass
column over that of the superclass, such as querying above
for <tt class="docutils literal"><span class="pre">Engineer.id</span></tt>.  Prior to 0.7 this was the reverse.</p>
</div>
<div class="section" id="single-table-inheritance">
<span id="declarative-single-table"></span><h3>Single Table Inheritance<a class="headerlink" href="#single-table-inheritance" title="Permalink to this headline">¶</a></h3>
<p>Single table inheritance is defined as a subclass that does not have
its own table; you just leave out the <tt class="docutils literal"><span class="pre">__table__</span></tt> and <tt class="docutils literal"><span class="pre">__tablename__</span></tt>
attributes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;people&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">discriminator</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span> <span class="n">discriminator</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span>
    <span class="n">primary_language</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span></pre></div>
</div>
<p>When the above mappers are configured, the <tt class="docutils literal"><span class="pre">Person</span></tt> class is mapped
to the <tt class="docutils literal"><span class="pre">people</span></tt> table <em>before</em> the <tt class="docutils literal"><span class="pre">primary_language</span></tt> column is
defined, and this column will not be included in its own mapping.
When <tt class="docutils literal"><span class="pre">Engineer</span></tt> then defines the <tt class="docutils literal"><span class="pre">primary_language</span></tt> column, the
column is added to the <tt class="docutils literal"><span class="pre">people</span></tt> table so that it is included in the
mapping for <tt class="docutils literal"><span class="pre">Engineer</span></tt> and is also part of the table&#8217;s full set of
columns.  Columns which are not mapped to <tt class="docutils literal"><span class="pre">Person</span></tt> are also excluded
from any other single or joined inheriting classes using the
<tt class="docutils literal"><span class="pre">exclude_properties</span></tt> mapper argument.  Below, <tt class="docutils literal"><span class="pre">Manager</span></tt> will have
all the attributes of <tt class="docutils literal"><span class="pre">Person</span></tt> and <tt class="docutils literal"><span class="pre">Manager</span></tt> but <em>not</em> the
<tt class="docutils literal"><span class="pre">primary_language</span></tt> attribute of <tt class="docutils literal"><span class="pre">Engineer</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;manager&#39;</span><span class="p">}</span>
    <span class="n">golf_swing</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span></pre></div>
</div>
<p>The attribute exclusion logic is provided by the
<tt class="docutils literal"><span class="pre">exclude_properties</span></tt> mapper argument, and declarative&#8217;s default
behavior can be disabled by passing an explicit <tt class="docutils literal"><span class="pre">exclude_properties</span></tt>
collection (empty or otherwise) to the <tt class="docutils literal"><span class="pre">__mapper_args__</span></tt>.</p>
<div class="section" id="resolving-column-conflicts">
<h4>Resolving Column Conflicts<a class="headerlink" href="#resolving-column-conflicts" title="Permalink to this headline">¶</a></h4>
<p>Note above that the <tt class="docutils literal"><span class="pre">primary_language</span></tt> and <tt class="docutils literal"><span class="pre">golf_swing</span></tt> columns
are &#8220;moved up&#8221; to be applied to <tt class="docutils literal"><span class="pre">Person.__table__</span></tt>, as a result of their
declaration on a subclass that has no table of its own.   A tricky case
comes up when two subclasses want to specify <em>the same</em> column, as below:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;people&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">discriminator</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span> <span class="n">discriminator</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span>
    <span class="n">start_date</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;manager&#39;</span><span class="p">}</span>
    <span class="n">start_date</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">)</span></pre></div>
</div>
<p>Above, the <tt class="docutils literal"><span class="pre">start_date</span></tt> column declared on both <tt class="docutils literal"><span class="pre">Engineer</span></tt> and <tt class="docutils literal"><span class="pre">Manager</span></tt>
will result in an error:</p>
<div class="highlight-python"><pre>sqlalchemy.exc.ArgumentError: Column 'start_date' on class
&lt;class '__main__.Manager'&gt; conflicts with existing
column 'people.start_date'</pre>
</div>
<p>In a situation like this, Declarative can&#8217;t be sure
of the intent, especially if the <tt class="docutils literal"><span class="pre">start_date</span></tt> columns had, for example,
different types.   A situation like this can be resolved by using
<a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> to define the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> conditionally, taking
care to return the <strong>existing column</strong> via the parent <tt class="docutils literal"><span class="pre">__table__</span></tt> if it
already exists:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declared_attr</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;people&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">discriminator</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span> <span class="n">discriminator</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">start_date</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="s">&quot;Start date column, if not present already.&quot;</span>
        <span class="k">return</span> <span class="n">Person</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;start_date&#39;</span><span class="p">,</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">))</span>

<span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;manager&#39;</span><span class="p">}</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">start_date</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="s">&quot;Start date column, if not present already.&quot;</span>
        <span class="k">return</span> <span class="n">Person</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;start_date&#39;</span><span class="p">,</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">))</span></pre></div>
</div>
<p>Above, when <tt class="docutils literal"><span class="pre">Manager</span></tt> is mapped, the <tt class="docutils literal"><span class="pre">start_date</span></tt> column is
already present on the <tt class="docutils literal"><span class="pre">Person</span></tt> class.  Declarative lets us return
that <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> as a result in this case, where it knows to skip
re-assigning the same column. If the mapping is mis-configured such
that the <tt class="docutils literal"><span class="pre">start_date</span></tt> column is accidentally re-assigned to a
different table (such as, if we changed <tt class="docutils literal"><span class="pre">Manager</span></tt> to be joined
inheritance without fixing <tt class="docutils literal"><span class="pre">start_date</span></tt>), an error is raised which
indicates an existing <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> is trying to be re-assigned to
a different owning <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.</p>
<p class="versionadded">
<span class="versionmodified">New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> can be used on a non-mixin
class, and the returned <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> or other mapped attribute
will be applied to the mapping as any other attribute.  Previously,
the resulting attribute would be ignored, and also result in a warning
being emitted when a subclass was created.</p>
<p class="versionadded">
<span class="versionmodified">New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a>, when used either with a
mixin or non-mixin declarative class, can return an existing
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> already assigned to the parent <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>,
to indicate that the re-assignment of the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> should be
skipped, however should still be mapped on the target class,
in order to resolve duplicate column conflicts.</p>
<p>The same concept can be used with mixin classes (see
<a class="reference internal" href="#declarative-mixins"><em>Mixin and Custom Base Classes</em></a>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;people&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">discriminator</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span> <span class="n">discriminator</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">HasStartDate</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">start_date</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__table__</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;start_date&#39;</span><span class="p">,</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">))</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">HasStartDate</span><span class="p">,</span> <span class="n">Person</span><span class="p">):</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">HasStartDate</span><span class="p">,</span> <span class="n">Person</span><span class="p">):</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;manager&#39;</span><span class="p">}</span></pre></div>
</div>
<p>The above mixin checks the local <tt class="docutils literal"><span class="pre">__table__</span></tt> attribute for the column.
Because we&#8217;re using single table inheritance, we&#8217;re sure that in this case,
<tt class="docutils literal"><span class="pre">cls.__table__</span></tt> refers to <tt class="docutils literal"><span class="pre">People.__table__</span></tt>.  If we were mixing joined-
and single-table inheritance, we might want our mixin to check more carefully
if <tt class="docutils literal"><span class="pre">cls.__table__</span></tt> is really the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> we&#8217;re looking for.</p>
</div>
</div>
<div class="section" id="concrete-table-inheritance">
<h3>Concrete Table Inheritance<a class="headerlink" href="#concrete-table-inheritance" title="Permalink to this headline">¶</a></h3>
<p>Concrete is defined as a subclass which has its own table and sets the
<tt class="docutils literal"><span class="pre">concrete</span></tt> keyword argument to <tt class="docutils literal"><span class="pre">True</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;people&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;engineers&#39;</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;concrete&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">primary_language</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span></pre></div>
</div>
<p>Usage of an abstract base class is a little less straightforward as it
requires usage of <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.util.polymorphic_union" title="sqlalchemy.orm.util.polymorphic_union"><tt class="xref py py-func docutils literal"><span class="pre">polymorphic_union()</span></tt></a>,
which needs to be created with the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects
before the class is built:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">engineers</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;engineers&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
                <span class="n">Column</span><span class="p">(</span><span class="s">&#39;id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
                <span class="n">Column</span><span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">)),</span>
                <span class="n">Column</span><span class="p">(</span><span class="s">&#39;primary_language&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
            <span class="p">)</span>
<span class="n">managers</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;managers&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
                <span class="n">Column</span><span class="p">(</span><span class="s">&#39;id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
                <span class="n">Column</span><span class="p">(</span><span class="s">&#39;name&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">)),</span>
                <span class="n">Column</span><span class="p">(</span><span class="s">&#39;golf_swing&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
            <span class="p">)</span>

<span class="n">punion</span> <span class="o">=</span> <span class="n">polymorphic_union</span><span class="p">({</span>
    <span class="s">&#39;engineer&#39;</span><span class="p">:</span><span class="n">engineers</span><span class="p">,</span>
    <span class="s">&#39;manager&#39;</span><span class="p">:</span><span class="n">managers</span>
<span class="p">},</span> <span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="s">&#39;punion&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__table__</span> <span class="o">=</span> <span class="n">punion</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span><span class="n">punion</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">type</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__table__</span> <span class="o">=</span> <span class="n">engineers</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span><span class="s">&#39;engineer&#39;</span><span class="p">,</span> <span class="s">&#39;concrete&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__table__</span> <span class="o">=</span> <span class="n">managers</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span><span class="s">&#39;manager&#39;</span><span class="p">,</span> <span class="s">&#39;concrete&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span></pre></div>
</div>
<div class="section" id="using-the-concrete-helpers">
<span id="declarative-concrete-helpers"></span><h4>Using the Concrete Helpers<a class="headerlink" href="#using-the-concrete-helpers" title="Permalink to this headline">¶</a></h4>
<p>Helper classes provides a simpler pattern for concrete inheritance.
With these objects, the <tt class="docutils literal"><span class="pre">__declare_last__</span></tt> helper is used to configure the
&#8220;polymorphic&#8221; loader for the mapper after all subclasses have been declared.</p>
<p class="versionadded">
<span class="versionmodified">New in version 0.7.3.</span></p>
<p>An abstract base can be declared using the
<a class="reference internal" href="#sqlalchemy.ext.declarative.AbstractConcreteBase" title="sqlalchemy.ext.declarative.AbstractConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">AbstractConcreteBase</span></tt></a> class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">AbstractConcreteBase</span>

<span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">AbstractConcreteBase</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="k">pass</span></pre></div>
</div>
<p>To have a concrete <tt class="docutils literal"><span class="pre">employee</span></tt> table, use <a class="reference internal" href="#sqlalchemy.ext.declarative.ConcreteBase" title="sqlalchemy.ext.declarative.ConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">ConcreteBase</span></tt></a> instead:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">ConcreteBase</span>

<span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">ConcreteBase</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;employee&#39;</span>
    <span class="n">employee_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span><span class="s">&#39;employee&#39;</span><span class="p">,</span>
                    <span class="s">&#39;concrete&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span></pre></div>
</div>
<p>Either <tt class="docutils literal"><span class="pre">Employee</span></tt> base can be used in the normal fashion:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">Employee</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;manager&#39;</span>
    <span class="n">employee_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">manager_data</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">40</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span><span class="s">&#39;manager&#39;</span><span class="p">,</span>
                    <span class="s">&#39;concrete&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Employee</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;engineer&#39;</span>
    <span class="n">employee_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">engineer_info</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">40</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span><span class="s">&#39;engineer&#39;</span><span class="p">,</span>
                    <span class="s">&#39;concrete&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span></pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="mixin-and-custom-base-classes">
<span id="declarative-mixins"></span><h2>Mixin and Custom Base Classes<a class="headerlink" href="#mixin-and-custom-base-classes" title="Permalink to this headline">¶</a></h2>
<p>A common need when using <a class="reference internal" href="#module-sqlalchemy.ext.declarative" title="sqlalchemy.ext.declarative"><tt class="xref py py-mod docutils literal"><span class="pre">declarative</span></tt></a> is to
share some functionality, such as a set of common columns, some common
table options, or other mapped properties, across many
classes.  The standard Python idioms for this is to have the classes
inherit from a base which includes these common features.</p>
<p>When using <a class="reference internal" href="#module-sqlalchemy.ext.declarative" title="sqlalchemy.ext.declarative"><tt class="xref py py-mod docutils literal"><span class="pre">declarative</span></tt></a>, this idiom is allowed
via the usage of a custom declarative base class, as well as a &#8220;mixin&#8221; class
which is inherited from in addition to the primary base.  Declarative
includes several helper features to make this work in terms of how
mappings are declared.   An example of some commonly mixed-in
idioms is below:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declared_attr</span>

<span class="k">class</span> <span class="nc">MyMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__tablename__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="n">__table_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;mysql_engine&#39;</span><span class="p">:</span> <span class="s">&#39;InnoDB&#39;</span><span class="p">}</span>
    <span class="n">__mapper_args__</span><span class="o">=</span> <span class="p">{</span><span class="s">&#39;always_refresh&#39;</span><span class="p">:</span> <span class="bp">True</span><span class="p">}</span>

    <span class="nb">id</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">MyMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span></pre></div>
</div>
<p>Where above, the class <tt class="docutils literal"><span class="pre">MyModel</span></tt> will contain an &#8220;id&#8221; column
as the primary key, a <tt class="docutils literal"><span class="pre">__tablename__</span></tt> attribute that derives
from the name of the class itself, as well as <tt class="docutils literal"><span class="pre">__table_args__</span></tt>
and <tt class="docutils literal"><span class="pre">__mapper_args__</span></tt> defined by the <tt class="docutils literal"><span class="pre">MyMixin</span></tt> mixin class.</p>
<p>There&#8217;s no fixed convention over whether <tt class="docutils literal"><span class="pre">MyMixin</span></tt> precedes
<tt class="docutils literal"><span class="pre">Base</span></tt> or not.  Normal Python method resolution rules apply, and
the above example would work just as well with:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">Base</span><span class="p">,</span> <span class="n">MyMixin</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span></pre></div>
</div>
<p>This works because <tt class="docutils literal"><span class="pre">Base</span></tt> here doesn&#8217;t define any of the
variables that <tt class="docutils literal"><span class="pre">MyMixin</span></tt> defines, i.e. <tt class="docutils literal"><span class="pre">__tablename__</span></tt>,
<tt class="docutils literal"><span class="pre">__table_args__</span></tt>, <tt class="docutils literal"><span class="pre">id</span></tt>, etc.   If the <tt class="docutils literal"><span class="pre">Base</span></tt> did define
an attribute of the same name, the class placed first in the
inherits list would determine which attribute is used on the
newly defined class.</p>
<div class="section" id="augmenting-the-base">
<h3>Augmenting the Base<a class="headerlink" href="#augmenting-the-base" title="Permalink to this headline">¶</a></h3>
<p>In addition to using a pure mixin, most of the techniques in this
section can also be applied to the base class itself, for patterns that
should apply to all classes derived from a particular base.  This is achieved
using the <tt class="docutils literal"><span class="pre">cls</span></tt> argument of the <a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a> function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declared_attr</span>

<span class="k">class</span> <span class="nc">Base</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__tablename__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="n">__table_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;mysql_engine&#39;</span><span class="p">:</span> <span class="s">&#39;InnoDB&#39;</span><span class="p">}</span>

    <span class="nb">id</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>

<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">(</span><span class="n">cls</span><span class="o">=</span><span class="n">Base</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span></pre></div>
</div>
<p>Where above, <tt class="docutils literal"><span class="pre">MyModel</span></tt> and all other classes that derive from <tt class="docutils literal"><span class="pre">Base</span></tt> will
have a table name derived from the class name, an <tt class="docutils literal"><span class="pre">id</span></tt> primary key column,
as well as the &#8220;InnoDB&#8221; engine for MySQL.</p>
</div>
<div class="section" id="mixing-in-columns">
<h3>Mixing in Columns<a class="headerlink" href="#mixing-in-columns" title="Permalink to this headline">¶</a></h3>
<p>The most basic way to specify a column on a mixin is by simple
declaration:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">TimestampMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">created_at</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">DateTime</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">func</span><span class="o">.</span><span class="n">now</span><span class="p">())</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">TimestampMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;test&#39;</span>

    <span class="nb">id</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span></pre></div>
</div>
<p>Where above, all declarative classes that include <tt class="docutils literal"><span class="pre">TimestampMixin</span></tt>
will also have a column <tt class="docutils literal"><span class="pre">created_at</span></tt> that applies a timestamp to
all row insertions.</p>
<p>Those familiar with the SQLAlchemy expression language know that
the object identity of clause elements defines their role in a schema.
Two <tt class="docutils literal"><span class="pre">Table</span></tt> objects <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> may both have a column called
<tt class="docutils literal"><span class="pre">id</span></tt>, but the way these are differentiated is that <tt class="docutils literal"><span class="pre">a.c.id</span></tt>
and <tt class="docutils literal"><span class="pre">b.c.id</span></tt> are two distinct Python objects, referencing their
parent tables <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> respectively.</p>
<p>In the case of the mixin column, it seems that only one
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object is explicitly created, yet the ultimate
<tt class="docutils literal"><span class="pre">created_at</span></tt> column above must exist as a distinct Python object
for each separate destination class.  To accomplish this, the declarative
extension creates a <strong>copy</strong> of each <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object encountered on
a class that is detected as a mixin.</p>
<p>This copy mechanism is limited to simple columns that have no foreign
keys, as a <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> itself contains references to columns
which can&#8217;t be properly recreated at this level.  For columns that
have foreign keys, as well as for the variety of mapper-level constructs
that require destination-explicit context, the
<a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> decorator is provided so that
patterns common to many classes can be defined as callables:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declared_attr</span>

<span class="k">class</span> <span class="nc">ReferenceAddressMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">address_id</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;address.id&#39;</span><span class="p">))</span>

<span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">ReferenceAddressMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;user&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
<p>Where above, the <tt class="docutils literal"><span class="pre">address_id</span></tt> class-level callable is executed at the
point at which the <tt class="docutils literal"><span class="pre">User</span></tt> class is constructed, and the declarative
extension can use the resulting <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object as returned by
the method without the need to copy it.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version >: </span>0.6.5
Rename 0.6.5 <tt class="docutils literal"><span class="pre">sqlalchemy.util.classproperty</span></tt>
into <a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a>.</p>
<p>Columns generated by <a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> can also be
referenced by <tt class="docutils literal"><span class="pre">__mapper_args__</span></tt> to a limited degree, currently
by <tt class="docutils literal"><span class="pre">polymorphic_on</span></tt> and <tt class="docutils literal"><span class="pre">version_id_col</span></tt>, by specifying the
classdecorator itself into the dictionary - the declarative extension
will resolve them at class construction time:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyMixin</span><span class="p">:</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">type_</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>

    <span class="n">__mapper_args__</span><span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span><span class="n">type_</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">MyMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span><span class="o">=</span><span class="s">&#39;test&#39;</span>
    <span class="nb">id</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="mixing-in-relationships">
<h3>Mixing in Relationships<a class="headerlink" href="#mixing-in-relationships" title="Permalink to this headline">¶</a></h3>
<p>Relationships created by <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a> are provided
with declarative mixin classes exclusively using the
<a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> approach, eliminating any ambiguity
which could arise when copying a relationship and its possibly column-bound
contents. Below is an example which combines a foreign key column and a
relationship so that two classes <tt class="docutils literal"><span class="pre">Foo</span></tt> and <tt class="docutils literal"><span class="pre">Bar</span></tt> can both be configured to
reference a common target class via many-to-one:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">RefTargetMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">target_id</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;target_id&#39;</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;target.id&#39;</span><span class="p">))</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">target</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Target&quot;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="n">RefTargetMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;foo&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Bar</span><span class="p">(</span><span class="n">RefTargetMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;bar&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">Target</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;target&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
<p><a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a> definitions which require explicit
primaryjoin, order_by etc. expressions should use the string forms
for these arguments, so that they are evaluated as late as possible.
To reference the mixin class in these expressions, use the given <tt class="docutils literal"><span class="pre">cls</span></tt>
to get its name:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">RefTargetMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">target_id</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;target_id&#39;</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;target.id&#39;</span><span class="p">))</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">target</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Target&quot;</span><span class="p">,</span>
            <span class="n">primaryjoin</span><span class="o">=</span><span class="s">&quot;Target.id==</span><span class="si">%s</span><span class="s">.target_id&quot;</span> <span class="o">%</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span>
        <span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="mixing-in-deferred-column-property-and-other-mapperproperty-classes">
<h3>Mixing in deferred(), column_property(), and other MapperProperty classes<a class="headerlink" href="#mixing-in-deferred-column-property-and-other-mapperproperty-classes" title="Permalink to this headline">¶</a></h3>
<p>Like <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>, all
<a class="reference internal" href="../internals.html#sqlalchemy.orm.interfaces.MapperProperty" title="sqlalchemy.orm.interfaces.MapperProperty"><tt class="xref py py-class docutils literal"><span class="pre">MapperProperty</span></tt></a> subclasses such as
<a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.deferred" title="sqlalchemy.orm.deferred"><tt class="xref py py-func docutils literal"><span class="pre">deferred()</span></tt></a>, <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.column_property" title="sqlalchemy.orm.column_property"><tt class="xref py py-func docutils literal"><span class="pre">column_property()</span></tt></a>,
etc. ultimately involve references to columns, and therefore, when
used with declarative mixins, have the <a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a>
requirement so that no reliance on copying is needed:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">SomethingMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">dprop</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">deferred</span><span class="p">(</span><span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">))</span>

<span class="k">class</span> <span class="nc">Something</span><span class="p">(</span><span class="n">SomethingMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&quot;something&quot;</span></pre></div>
</div>
</div>
<div class="section" id="mixing-in-association-proxy-and-other-attributes">
<h3>Mixing in Association Proxy and Other Attributes<a class="headerlink" href="#mixing-in-association-proxy-and-other-attributes" title="Permalink to this headline">¶</a></h3>
<p>Mixins can specify user-defined attributes as well as other extension
units such as <a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a>.   The usage of
<a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> is required in those cases where the attribute must
be tailored specifically to the target subclass.   An example is when
constructing multiple <a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a> attributes which each
target a different type of child object.  Below is an
<a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a> / mixin example which provides a scalar list of
string values to an implementing class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">Column</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">,</span> <span class="n">String</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="n">relationship</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.associationproxy</span> <span class="kn">import</span> <span class="n">association_proxy</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span><span class="p">,</span> <span class="n">declared_attr</span>

<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">HasStringCollection</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">_strings</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">class</span> <span class="nc">StringAttribute</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
            <span class="n">__tablename__</span> <span class="o">=</span> <span class="n">cls</span><span class="o">.</span><span class="n">string_table_name</span>
            <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">),</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
            <span class="n">parent_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span>
                            <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">.id&#39;</span> <span class="o">%</span> <span class="n">cls</span><span class="o">.</span><span class="n">__tablename__</span><span class="p">),</span>
                            <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
            <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>

        <span class="k">return</span> <span class="n">relationship</span><span class="p">(</span><span class="n">StringAttribute</span><span class="p">)</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">strings</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">association_proxy</span><span class="p">(</span><span class="s">&#39;_strings&#39;</span><span class="p">,</span> <span class="s">&#39;value&#39;</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">TypeA</span><span class="p">(</span><span class="n">HasStringCollection</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;type_a&#39;</span>
    <span class="n">string_table_name</span> <span class="o">=</span> <span class="s">&#39;type_a_strings&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">(),</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="k">class</span> <span class="nc">TypeB</span><span class="p">(</span><span class="n">HasStringCollection</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;type_b&#39;</span>
    <span class="n">string_table_name</span> <span class="o">=</span> <span class="s">&#39;type_b_strings&#39;</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">(),</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
<p>Above, the <tt class="docutils literal"><span class="pre">HasStringCollection</span></tt> mixin produces a <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>
which refers to a newly generated class called <tt class="docutils literal"><span class="pre">StringAttribute</span></tt>.  The
<tt class="docutils literal"><span class="pre">StringAttribute</span></tt> class is generated with it&#8217;s own <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
definition which is local to the parent class making usage of the
<tt class="docutils literal"><span class="pre">HasStringCollection</span></tt> mixin.  It also produces an <a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a>
object which proxies references to the <tt class="docutils literal"><span class="pre">strings</span></tt> attribute onto the <tt class="docutils literal"><span class="pre">value</span></tt>
attribute of each <tt class="docutils literal"><span class="pre">StringAttribute</span></tt> instance.</p>
<p><tt class="docutils literal"><span class="pre">TypeA</span></tt> or <tt class="docutils literal"><span class="pre">TypeB</span></tt> can be instantiated given the constructor
argument <tt class="docutils literal"><span class="pre">strings</span></tt>, a list of strings:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ta</span> <span class="o">=</span> <span class="n">TypeA</span><span class="p">(</span><span class="n">strings</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;foo&#39;</span><span class="p">,</span> <span class="s">&#39;bar&#39;</span><span class="p">])</span>
<span class="n">tb</span> <span class="o">=</span> <span class="n">TypeA</span><span class="p">(</span><span class="n">strings</span><span class="o">=</span><span class="p">[</span><span class="s">&#39;bat&#39;</span><span class="p">,</span> <span class="s">&#39;bar&#39;</span><span class="p">])</span></pre></div>
</div>
<p>This list will generate a collection
of <tt class="docutils literal"><span class="pre">StringAttribute</span></tt> objects, which are persisted into a table that&#8217;s
local to either the <tt class="docutils literal"><span class="pre">type_a_strings</span></tt> or <tt class="docutils literal"><span class="pre">type_b_strings</span></tt> table:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">ta</span><span class="o">.</span><span class="n">_strings</span>
<span class="go">[&lt;__main__.StringAttribute object at 0x10151cd90&gt;,</span>
<span class="go">    &lt;__main__.StringAttribute object at 0x10151ce10&gt;]</span></pre></div>
</div>
<p>When constructing the <a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a>, the
<a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> decorator must be used so that a distinct
<a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a> object is created for each of the <tt class="docutils literal"><span class="pre">TypeA</span></tt>
and <tt class="docutils literal"><span class="pre">TypeB</span></tt> classes.</p>
<p class="versionadded">
<span class="versionmodified">New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> is usable with non-mapped
attributes, including user-defined attributes as well as
<a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a>.</p>
</div>
<div class="section" id="controlling-table-inheritance-with-mixins">
<h3>Controlling table inheritance with mixins<a class="headerlink" href="#controlling-table-inheritance-with-mixins" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">__tablename__</span></tt> attribute in conjunction with the hierarchy of
classes involved in a declarative mixin scenario controls what type of
table inheritance, if any,
is configured by the declarative extension.</p>
<p>If the <tt class="docutils literal"><span class="pre">__tablename__</span></tt> is computed by a mixin, you may need to
control which classes get the computed attribute in order to get the
type of table inheritance you require.</p>
<p>For example, if you had a mixin that computes <tt class="docutils literal"><span class="pre">__tablename__</span></tt> but
where you wanted to use that mixin in a single table inheritance
hierarchy, you can explicitly specify <tt class="docutils literal"><span class="pre">__tablename__</span></tt> as <tt class="docutils literal"><span class="pre">None</span></tt> to
indicate that the class should not have a table mapped:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declared_attr</span>

<span class="k">class</span> <span class="nc">Tablename</span><span class="p">:</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__tablename__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Tablename</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">discriminator</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span> <span class="n">discriminator</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="bp">None</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span>
    <span class="n">primary_language</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span></pre></div>
</div>
<p>Alternatively, you can make the mixin intelligent enough to only
return a <tt class="docutils literal"><span class="pre">__tablename__</span></tt> in the event that no table is already
mapped in the inheritance hierarchy. To help with this, a
<a class="reference internal" href="#sqlalchemy.ext.declarative.has_inherited_table" title="sqlalchemy.ext.declarative.has_inherited_table"><tt class="xref py py-func docutils literal"><span class="pre">has_inherited_table()</span></tt></a> helper
function is provided that returns <tt class="docutils literal"><span class="pre">True</span></tt> if a parent class already
has a mapped table.</p>
<p>As an example, here&#8217;s a mixin that will only allow single table
inheritance:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declared_attr</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">has_inherited_table</span>

<span class="k">class</span> <span class="nc">Tablename</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__tablename__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">has_inherited_table</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">None</span>
        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Tablename</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">discriminator</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span> <span class="n">discriminator</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">primary_language</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span></pre></div>
</div>
<p>If you want to use a similar pattern with a mix of single and joined
table inheritance, you would need a slightly different mixin and use
it on any joined table child classes in addition to their parent
classes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declared_attr</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">has_inherited_table</span>

<span class="k">class</span> <span class="nc">Tablename</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__tablename__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">has_inherited_table</span><span class="p">(</span><span class="n">cls</span><span class="p">)</span> <span class="ow">and</span>
            <span class="n">Tablename</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">cls</span><span class="o">.</span><span class="n">__bases__</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">None</span>
        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Tablename</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">discriminator</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;type&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_on&#39;</span><span class="p">:</span> <span class="n">discriminator</span><span class="p">}</span>

<span class="c"># This is single table inheritance</span>
<span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Person</span><span class="p">):</span>
    <span class="n">primary_language</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span>

<span class="c"># This is joined table inheritance</span>
<span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">Tablename</span><span class="p">,</span> <span class="n">Person</span><span class="p">):</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;person.id&#39;</span><span class="p">),</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">preferred_recreation</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span> <span class="s">&#39;engineer&#39;</span><span class="p">}</span></pre></div>
</div>
</div>
<div class="section" id="combining-table-mapper-arguments-from-multiple-mixins">
<h3>Combining Table/Mapper Arguments from Multiple Mixins<a class="headerlink" href="#combining-table-mapper-arguments-from-multiple-mixins" title="Permalink to this headline">¶</a></h3>
<p>In the case of <tt class="docutils literal"><span class="pre">__table_args__</span></tt> or <tt class="docutils literal"><span class="pre">__mapper_args__</span></tt>
specified with declarative mixins, you may want to combine
some parameters from several mixins with those you wish to
define on the class iteself. The
<a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> decorator can be used
here to create user-defined collation routines that pull
from multiple collections:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declared_attr</span>

<span class="k">class</span> <span class="nc">MySQLSettings</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">__table_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;mysql_engine&#39;</span><span class="p">:</span><span class="s">&#39;InnoDB&#39;</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">MyOtherMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">__table_args__</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;info&#39;</span><span class="p">:</span><span class="s">&#39;foo&#39;</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">MySQLSettings</span><span class="p">,</span> <span class="n">MyOtherMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span><span class="o">=</span><span class="s">&#39;my_model&#39;</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__table_args__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="n">args</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">()</span>
        <span class="n">args</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">MySQLSettings</span><span class="o">.</span><span class="n">__table_args__</span><span class="p">)</span>
        <span class="n">args</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">MyOtherMixin</span><span class="o">.</span><span class="n">__table_args__</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">args</span>

    <span class="nb">id</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="creating-indexes-with-mixins">
<h3>Creating Indexes with Mixins<a class="headerlink" href="#creating-indexes-with-mixins" title="Permalink to this headline">¶</a></h3>
<p>To define a named, potentially multicolumn <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> that applies to all
tables derived from a mixin, use the &#8220;inline&#8221; form of <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> and
establish it as part of <tt class="docutils literal"><span class="pre">__table_args__</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="n">a</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">)</span>
    <span class="n">b</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">)</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__table_args__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">Index</span><span class="p">(</span><span class="s">&#39;test_idx_</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">cls</span><span class="o">.</span><span class="n">__tablename__</span><span class="p">,</span> <span class="s">&#39;a&#39;</span><span class="p">,</span> <span class="s">&#39;b&#39;</span><span class="p">),)</span>

<span class="k">class</span> <span class="nc">MyModel</span><span class="p">(</span><span class="n">MyMixin</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;atable&#39;</span>
    <span class="n">c</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span><span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
</div>
</div>
<div class="section" id="special-directives">
<h2>Special Directives<a class="headerlink" href="#special-directives" title="Permalink to this headline">¶</a></h2>
<div class="section" id="declare-last">
<h3><tt class="docutils literal"><span class="pre">__declare_last__()</span></tt><a class="headerlink" href="#declare-last" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">__declare_last__()</span></tt> hook allows definition of
a class level function that is automatically called by the
<a class="reference internal" href="../events.html#sqlalchemy.orm.events.MapperEvents.after_configured" title="sqlalchemy.orm.events.MapperEvents.after_configured"><tt class="xref py py-meth docutils literal"><span class="pre">MapperEvents.after_configured()</span></tt></a> event, which occurs after mappings are
assumed to be completed and the &#8216;configure&#8217; step has finished:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">__declare_last__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="s">&quot;&quot;</span>
        <span class="c"># do something with mappings</span></pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 0.7.3.</span></p>
</div>
<div class="section" id="abstract">
<span id="declarative-abstract"></span><h3><tt class="docutils literal"><span class="pre">__abstract__</span></tt><a class="headerlink" href="#abstract" title="Permalink to this headline">¶</a></h3>
<p><tt class="docutils literal"><span class="pre">__abstract__</span></tt> causes declarative to skip the production
of a table or mapper for the class entirely.  A class can be added within a
hierarchy in the same way as mixin (see <a class="reference internal" href="#declarative-mixins"><em>Mixin and Custom Base Classes</em></a>), allowing
subclasses to extend just from the special class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">SomeAbstractBase</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__abstract__</span> <span class="o">=</span> <span class="bp">True</span>

    <span class="k">def</span> <span class="nf">some_helpful_method</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;&quot;</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__mapper_args__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;helpful mapper arguments&quot;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">MyMappedClass</span><span class="p">(</span><span class="n">SomeAbstractBase</span><span class="p">):</span>
    <span class="s">&quot;&quot;</span></pre></div>
</div>
<p>One possible use of <tt class="docutils literal"><span class="pre">__abstract__</span></tt> is to use a distinct
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> for different bases:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">DefaultBase</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__abstract__</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">OtherBase</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="n">__abstract__</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span></pre></div>
</div>
<p>Above, classes which inherit from <tt class="docutils literal"><span class="pre">DefaultBase</span></tt> will use one
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> as the registry of tables, and those which inherit from
<tt class="docutils literal"><span class="pre">OtherBase</span></tt> will use a different one. The tables themselves can then be
created perhaps within distinct databases:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">DefaultBase</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="n">some_engine</span><span class="p">)</span>
<span class="n">OtherBase</span><span class="o">.</span><span class="n">metadata_create_all</span><span class="p">(</span><span class="n">some_other_engine</span><span class="p">)</span></pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 0.7.3.</span></p>
</div>
</div>
<div class="section" id="class-constructor">
<h2>Class Constructor<a class="headerlink" href="#class-constructor" title="Permalink to this headline">¶</a></h2>
<p>As a convenience feature, the <a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a> sets a default
constructor on classes which takes keyword arguments, and assigns them
to the named attributes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">e</span> <span class="o">=</span> <span class="n">Engineer</span><span class="p">(</span><span class="n">primary_language</span><span class="o">=</span><span class="s">&#39;python&#39;</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="sessions">
<h2>Sessions<a class="headerlink" href="#sessions" title="Permalink to this headline">¶</a></h2>
<p>Note that <tt class="docutils literal"><span class="pre">declarative</span></tt> does nothing special with sessions, and is
only intended as an easier way to configure mappers and
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects.  A typical application
setup using <tt class="xref py py-class docutils literal"><span class="pre">scoped_session</span></tt> might look like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;postgresql://scott:tiger@localhost/test&#39;</span><span class="p">)</span>
<span class="n">Session</span> <span class="o">=</span> <span class="n">scoped_session</span><span class="p">(</span><span class="n">sessionmaker</span><span class="p">(</span><span class="n">autocommit</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                                      <span class="n">autoflush</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                                      <span class="n">bind</span><span class="o">=</span><span class="n">engine</span><span class="p">))</span>
<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span></pre></div>
</div>
<p>Mapped instances then make usage of
<a class="reference internal" href="../session.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a> in the usual way.</p>
</div>
<div class="section" id="api-reference">
<h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="sqlalchemy.ext.declarative.declarative_base">
<tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">declarative_base</tt><big>(</big><em>bind=None</em>, <em>metadata=None</em>, <em>mapper=None</em>, <em>cls=&lt;type 'object'&gt;</em>, <em>name='Base'</em>, <em>constructor=&lt;function __init__ at 0x10aadd938&gt;</em>, <em>class_registry=None</em>, <em>metaclass=&lt;class 'sqlalchemy.ext.declarative.api.DeclarativeMeta'&gt;</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.declarative_base" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct a base class for declarative class definitions.</p>
<p>The new base class will be given a metaclass that produces
appropriate <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects and makes
the appropriate <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a> calls based on the
information provided declaratively in the class and any subclasses
of the class.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>bind</strong> &#8211; An optional
<tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt>, will be assigned
the <tt class="docutils literal"><span class="pre">bind</span></tt> attribute on the <tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt>
instance.</li>
<li><strong>metadata</strong> &#8211; An optional <tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt> instance.  All
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects implicitly declared by
subclasses of the base will share this MetaData.  A MetaData instance
will be created if none is provided.  The
<tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt> instance will be available via the
<cite>metadata</cite> attribute of the generated declarative base class.</li>
<li><strong>mapper</strong> &#8211; An optional callable, defaults to <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a>. Will
be used to map subclasses to their Tables.</li>
<li><strong>cls</strong> &#8211; Defaults to <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt>. A type to use as the base for the generated
declarative base class. May be a class or tuple of classes.</li>
<li><strong>name</strong> &#8211; Defaults to <tt class="docutils literal"><span class="pre">Base</span></tt>.  The display name for the generated
class.  Customizing this is not required, but can improve clarity in
tracebacks and debugging.</li>
<li><strong>constructor</strong> &#8211; Defaults to
<tt class="xref py py-func docutils literal"><span class="pre">_declarative_constructor()</span></tt>, an
__init__ implementation that assigns **kwargs for declared
fields and relationships to an instance.  If <tt class="docutils literal"><span class="pre">None</span></tt> is supplied,
no __init__ will be provided and construction will fall back to
cls.__init__ by way of the normal Python semantics.</li>
<li><strong>class_registry</strong> &#8211; optional dictionary that will serve as the
registry of class names-&gt; mapped classes when string names
are used to identify classes inside of <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>
and others.  Allows two or more declarative base classes
to share the same registry of class names for simplified
inter-base relationships.</li>
<li><strong>metaclass</strong> &#8211; Defaults to <tt class="xref py py-class docutils literal"><span class="pre">DeclarativeMeta</span></tt>.  A metaclass or __metaclass__
compatible callable to use as the meta type of the generated
declarative base class.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.ext.declarative.declared_attr">
<em class="property">class </em><tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">declared_attr</tt><big>(</big><em>fget</em>, <em>*arg</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.declared_attr" title="Permalink to this definition">¶</a></dt>
<dd><p>Mark a class-level method as representing the definition of
a mapped property or special declarative member name.</p>
<p>&#64;declared_attr turns the attribute into a scalar-like
property that can be invoked from the uninstantiated class.
Declarative treats attributes specifically marked with
&#64;declared_attr as returning a construct that is specific
to mapping or declarative table configuration.  The name
of the attribute is that of what the non-dynamic version
of the attribute would be.</p>
<p>&#64;declared_attr is more often than not applicable to mixins,
to define relationships that are to be applied to different
implementors of the class:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ProvidesUser</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="s">&quot;A mixin that adds a &#39;user&#39; relationship to classes.&quot;</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">user</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;User&quot;</span><span class="p">)</span></pre></div>
</div>
<p>It also can be applied to mapped classes, such as to provide
a &#8220;polymorphic&#8221; scheme for inheritance:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="nb">type</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">),</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__tablename__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

    <span class="nd">@declared_attr</span>
    <span class="k">def</span> <span class="nf">__mapper_args__</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">cls</span><span class="o">.</span><span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;Employee&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{</span>
                    <span class="s">&quot;polymorphic_on&quot;</span><span class="p">:</span><span class="n">cls</span><span class="o">.</span><span class="n">type</span><span class="p">,</span>
                    <span class="s">&quot;polymorphic_identity&quot;</span><span class="p">:</span><span class="s">&quot;Employee&quot;</span>
            <span class="p">}</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">{</span><span class="s">&quot;polymorphic_identity&quot;</span><span class="p">:</span><span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="p">}</span></pre></div>
</div>
<p class="versionchanged">
<span class="versionmodified">Changed in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> can be used with
non-ORM or extension attributes, such as user-defined attributes
or <a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a> objects, which will be assigned
to the class at class construction time.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.ext.declarative.api._declarative_constructor">
<tt class="descclassname">sqlalchemy.ext.declarative.api.</tt><tt class="descname">_declarative_constructor</tt><big>(</big><em>self</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.api._declarative_constructor" title="Permalink to this definition">¶</a></dt>
<dd><p>A simple constructor that allows initialization from kwargs.</p>
<p>Sets attributes on the constructed instance using the names and
values in <tt class="docutils literal"><span class="pre">kwargs</span></tt>.</p>
<p>Only keys that are present as
attributes of the instance&#8217;s class are allowed. These could be,
for example, any mapped columns or relationships.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.ext.declarative.has_inherited_table">
<tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">has_inherited_table</tt><big>(</big><em>cls</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.has_inherited_table" title="Permalink to this definition">¶</a></dt>
<dd><p>Given a class, return True if any of the classes it inherits from has a
mapped table, otherwise return False.</p>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.ext.declarative.synonym_for">
<tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">synonym_for</tt><big>(</big><em>name</em>, <em>map_column=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.synonym_for" title="Permalink to this definition">¶</a></dt>
<dd><p>Decorator, make a Python &#64;property a query synonym for a column.</p>
<p>A decorator version of <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.synonym" title="sqlalchemy.orm.synonym"><tt class="xref py py-func docutils literal"><span class="pre">synonym()</span></tt></a>. The function being
decorated is the &#8216;descriptor&#8217;, otherwise passes its arguments through to
synonym():</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@synonym_for</span><span class="p">(</span><span class="s">&#39;col&#39;</span><span class="p">)</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">prop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="k">return</span> <span class="s">&#39;special sauce&#39;</span></pre></div>
</div>
<p>The regular <tt class="docutils literal"><span class="pre">synonym()</span></tt> is also usable directly in a declarative setting
and may be convenient for read/write properties:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">prop</span> <span class="o">=</span> <span class="n">synonym</span><span class="p">(</span><span class="s">&#39;col&#39;</span><span class="p">,</span> <span class="n">descriptor</span><span class="o">=</span><span class="nb">property</span><span class="p">(</span><span class="n">_read_prop</span><span class="p">,</span> <span class="n">_write_prop</span><span class="p">))</span></pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.ext.declarative.comparable_using">
<tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">comparable_using</tt><big>(</big><em>comparator_factory</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.comparable_using" title="Permalink to this definition">¶</a></dt>
<dd><p>Decorator, allow a Python &#64;property to be used in query criteria.</p>
<p>This is a  decorator front end to
<tt class="xref py py-func docutils literal"><span class="pre">comparable_property()</span></tt> that passes
through the comparator_factory and the function being decorated:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@comparable_using</span><span class="p">(</span><span class="n">MyComparatorType</span><span class="p">)</span>
<span class="nd">@property</span>
<span class="k">def</span> <span class="nf">prop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="k">return</span> <span class="s">&#39;special sauce&#39;</span></pre></div>
</div>
<p>The regular <tt class="docutils literal"><span class="pre">comparable_property()</span></tt> is also usable directly in a
declarative setting and may be convenient for read/write properties:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">prop</span> <span class="o">=</span> <span class="n">comparable_property</span><span class="p">(</span><span class="n">MyComparatorType</span><span class="p">)</span></pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="sqlalchemy.ext.declarative.instrument_declarative">
<tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">instrument_declarative</tt><big>(</big><em>cls</em>, <em>registry</em>, <em>metadata</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.instrument_declarative" title="Permalink to this definition">¶</a></dt>
<dd><p>Given a class, configure the class declaratively,
using the given registry, which can be any dictionary, and
MetaData object.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.ext.declarative.AbstractConcreteBase">
<em class="property">class </em><tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">AbstractConcreteBase</tt><a class="headerlink" href="#sqlalchemy.ext.declarative.AbstractConcreteBase" title="Permalink to this definition">¶</a></dt>
<dd><p>A helper class for &#8216;concrete&#8217; declarative mappings.</p>
<p><a class="reference internal" href="#sqlalchemy.ext.declarative.AbstractConcreteBase" title="sqlalchemy.ext.declarative.AbstractConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">AbstractConcreteBase</span></tt></a> will use the <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.util.polymorphic_union" title="sqlalchemy.orm.util.polymorphic_union"><tt class="xref py py-func docutils literal"><span class="pre">polymorphic_union()</span></tt></a>
function automatically, against all tables mapped as a subclass
to this class.   The function is called via the
<tt class="docutils literal"><span class="pre">__declare_last__()</span></tt> function, which is essentially
a hook for the <tt class="xref py py-func docutils literal"><span class="pre">MapperEvents.after_configured()</span></tt> event.</p>
<p><a class="reference internal" href="#sqlalchemy.ext.declarative.AbstractConcreteBase" title="sqlalchemy.ext.declarative.AbstractConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">AbstractConcreteBase</span></tt></a> does not produce a mapped
table for the class itself.  Compare to <a class="reference internal" href="#sqlalchemy.ext.declarative.ConcreteBase" title="sqlalchemy.ext.declarative.ConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">ConcreteBase</span></tt></a>,
which does.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">AbstractConcreteBase</span>

<span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">AbstractConcreteBase</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="k">pass</span>

<span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">Employee</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;manager&#39;</span>
    <span class="n">employee_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">manager_data</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">40</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span><span class="s">&#39;manager&#39;</span><span class="p">,</span>
                    <span class="s">&#39;concrete&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span></pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.ext.declarative.ConcreteBase">
<em class="property">class </em><tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">ConcreteBase</tt><a class="headerlink" href="#sqlalchemy.ext.declarative.ConcreteBase" title="Permalink to this definition">¶</a></dt>
<dd><p>A helper class for &#8216;concrete&#8217; declarative mappings.</p>
<p><a class="reference internal" href="#sqlalchemy.ext.declarative.ConcreteBase" title="sqlalchemy.ext.declarative.ConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">ConcreteBase</span></tt></a> will use the <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.util.polymorphic_union" title="sqlalchemy.orm.util.polymorphic_union"><tt class="xref py py-func docutils literal"><span class="pre">polymorphic_union()</span></tt></a>
function automatically, against all tables mapped as a subclass
to this class.   The function is called via the
<tt class="docutils literal"><span class="pre">__declare_last__()</span></tt> function, which is essentially
a hook for the <tt class="xref py py-func docutils literal"><span class="pre">MapperEvents.after_configured()</span></tt> event.</p>
<p><a class="reference internal" href="#sqlalchemy.ext.declarative.ConcreteBase" title="sqlalchemy.ext.declarative.ConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">ConcreteBase</span></tt></a> produces a mapped
table for the class itself.  Compare to <a class="reference internal" href="#sqlalchemy.ext.declarative.AbstractConcreteBase" title="sqlalchemy.ext.declarative.AbstractConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">AbstractConcreteBase</span></tt></a>,
which does not.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">ConcreteBase</span>

<span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">ConcreteBase</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;employee&#39;</span>
    <span class="n">employee_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span><span class="s">&#39;employee&#39;</span><span class="p">,</span>
                    <span class="s">&#39;concrete&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span>

<span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">Employee</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;manager&#39;</span>
    <span class="n">employee_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span>
    <span class="n">manager_data</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">40</span><span class="p">))</span>
    <span class="n">__mapper_args__</span> <span class="o">=</span> <span class="p">{</span>
                    <span class="s">&#39;polymorphic_identity&#39;</span><span class="p">:</span><span class="s">&#39;manager&#39;</span><span class="p">,</span>
                    <span class="s">&#39;concrete&#39;</span><span class="p">:</span><span class="bp">True</span><span class="p">}</span></pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.ext.declarative.DeferredReflection">
<em class="property">class </em><tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">DeferredReflection</tt><a class="headerlink" href="#sqlalchemy.ext.declarative.DeferredReflection" title="Permalink to this definition">¶</a></dt>
<dd><p>A helper class for construction of mappings based on
a deferred reflection step.</p>
<p>Normally, declarative can be used with reflection by
setting a <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object using autoload=True
as the <tt class="docutils literal"><span class="pre">__table__</span></tt> attribute on a declarative class.
The caveat is that the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> must be fully
reflected, or at the very least have a primary key column,
at the point at which a normal declarative mapping is
constructed, meaning the <a class="reference internal" href="../../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> must be available
at class declaration time.</p>
<p>The <a class="reference internal" href="#sqlalchemy.ext.declarative.DeferredReflection" title="sqlalchemy.ext.declarative.DeferredReflection"><tt class="xref py py-class docutils literal"><span class="pre">DeferredReflection</span></tt></a> mixin moves the construction
of mappers to be at a later point, after a specific
method is called which first reflects all <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
objects created so far.   Classes can define it as such:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">DeferredReflection</span>
<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">DeferredReflection</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;mytable&#39;</span></pre></div>
</div>
<p>Above, <tt class="docutils literal"><span class="pre">MyClass</span></tt> is not yet mapped.   After a series of
classes have been defined in the above fashion, all tables
can be reflected and mappings created using
<tt class="xref py py-meth docutils literal"><span class="pre">DeferredReflection.prepare()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&quot;someengine://...&quot;</span><span class="p">)</span>
<span class="n">DeferredReflection</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span></pre></div>
</div>
<p>The <a class="reference internal" href="#sqlalchemy.ext.declarative.DeferredReflection" title="sqlalchemy.ext.declarative.DeferredReflection"><tt class="xref py py-class docutils literal"><span class="pre">DeferredReflection</span></tt></a> mixin can be applied to individual
classes, used as the base for the declarative base itself,
or used in a custom abstract class.   Using an abstract base
allows that only a subset of classes to be prepared for a
particular prepare step, which is necessary for applications
that use more than one engine.  For example, if an application
has two engines, you might use two bases, and prepare each
separately, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ReflectedOne</span><span class="p">(</span><span class="n">DeferredReflection</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__abstract__</span> <span class="o">=</span> <span class="bp">True</span>

<span class="k">class</span> <span class="nc">ReflectedTwo</span><span class="p">(</span><span class="n">DeferredReflection</span><span class="p">,</span> <span class="n">Base</span><span class="p">):</span>
    <span class="n">__abstract__</span> <span class="o">=</span> <span class="bp">True</span>

<span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">ReflectedOne</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;mytable&#39;</span>

<span class="k">class</span> <span class="nc">MyOtherClass</span><span class="p">(</span><span class="n">ReflectedOne</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;myothertable&#39;</span>

<span class="k">class</span> <span class="nc">YetAnotherClass</span><span class="p">(</span><span class="n">ReflectedTwo</span><span class="p">):</span>
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;yetanothertable&#39;</span>

<span class="c"># ... etc.</span></pre></div>
</div>
<p>Above, the class hierarchies for <tt class="docutils literal"><span class="pre">ReflectedOne</span></tt> and
<tt class="docutils literal"><span class="pre">ReflectedTwo</span></tt> can be configured separately:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ReflectedOne</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span><span class="n">engine_one</span><span class="p">)</span>
<span class="n">ReflectedTwo</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span><span class="n">engine_two</span><span class="p">)</span></pre></div>
</div>
<p class="versionadded">
<span class="versionmodified">New in version 0.8.</span></p>
</dd></dl>

</div>
</div>

    </div>

</div>

<div id="docs-bottom-navigation" class="docs-navigation-links">
        Previous:
        <a href="associationproxy.html" title="previous chapter">Association Proxy</a>
        Next:
        <a href="mutable.html" title="next chapter">Mutation Tracking</a>

    <div id="docs-copyright">
        &copy; <a href="../../copyright.html">Copyright</a> 2007-2013, the SQLAlchemy authors and contributors.
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
</div>

</div>

        
    </body>
</html>


